package com.myapplication.app.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.myapplication.app.bean.Mess;
import com.myapplication.app.MyApp;
import com.myapplication.app.R;
import com.myapplication.app.bean.TryJsonUtil;
import com.myapplication.app.date.TestDate;
import me.maxwin.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tukela on 2015/3/3.
 */
public class FragmentBs  extends Fragment implements XListView.IXListViewListener, AbsListView.OnScrollListener {
    private View rootView;
    private XListView xListView;
    private List<Mess> messageList=new ArrayList<Mess>(0);
    private MyAdapter myAdapter=new MyAdapter();
    private Handler mHandler;


    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mHandler = new Handler();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_bs,null);
        xListView= (XListView) rootView.findViewById(R.id.xListView);
        xListView.setAdapter(myAdapter);
        xListView.setPullRefreshEnable(false);
        xListView.setPullLoadEnable(true);
        xListView.setXListViewListener(this);
        xListView.setOnScrollListener(this);
        loadDate();
        return rootView;
    }
    public void loadDate(){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loading();
            }
        }, 0);
    }

    private void loading(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("name", "value");
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, MyApp.BSURL
                ,
                params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                      //  testTextView.setText("reply: " + responseInfo.result);
                        messageList.addAll(TryJsonUtil.getMessList(responseInfo.result));
                        myAdapter.notifyDataSetChanged();
                        stopLoad();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        //testTextView.setText(error.getExceptionCode() + ":" + msg);
                        messageList.addAll(TryJsonUtil.getMessList(TestDate.mes));
                        myAdapter.notifyDataSetChanged();
                        stopLoad();
                    }
                });

    }
    /*
    * 停止加载
     */
    private void stopLoad() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        loadDate();
    }



    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return messageList.size()>0?messageList.size():0;
        }

        @Override
        public Object getItem(int position) {
            return messageList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView1, ViewGroup parent) {
            Mess mess=messageList.get(position);
            View convertView = LayoutInflater.from(getActivity())
                    .inflate(R.layout.fragment_msg_item, null);
            ImageView imgLeft = (ImageView) convertView.findViewById(R.id.img_left);
            TextView textView = (TextView) convertView.findViewById(R.id.text);
            ImageView imgRight = (ImageView) convertView.findViewById(R.id.img_right);
           // textView.setText(mess.getMessage());
            if (mess.getIs_raed().equals("1"))
            {}else {

            }

            return convertView;
        }
    }
}
